package edu.ptu.shortlink.admin.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import edu.ptu.shortlink.admin.common.biz.user.UserContext;
import edu.ptu.shortlink.admin.common.convention.exception.ServiceException;
import edu.ptu.shortlink.admin.common.convention.result.Result;
import edu.ptu.shortlink.admin.dao.entity.GroupDO;
import edu.ptu.shortlink.admin.dao.mapper.GroupMapper;
import edu.ptu.shortlink.admin.remote.ShortLinkRemoteService;
import edu.ptu.shortlink.admin.remote.dto.req.RecycleBinPageReqDTO;
import edu.ptu.shortlink.admin.remote.dto.resp.ShortLinkPageRespDTO;
import edu.ptu.shortlink.admin.service.IRecycleBinService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @BelongsProject: shortlink
 * @BelongsPackage: edu.ptu.shortlink.admin.service.impl
 * @Author: HYJ
 * @CreateTime: 2025-05-02  18:01
 * @Description: 回收箱服务实现类
 * @Version: 1.0
 */
@Service
@RequiredArgsConstructor
public class RecycleBinServiceImpl implements IRecycleBinService {

   private final ShortLinkRemoteService shortLinkRemoteService;

    private final GroupMapper groupMapper;

    @Override
    public Result<Page<ShortLinkPageRespDTO>> pageRecycleBin(RecycleBinPageReqDTO requestParam) {

        LambdaQueryWrapper<GroupDO> queryWrapper = Wrappers.lambdaQuery(GroupDO.class)
                .eq(GroupDO::getUsername, UserContext.getUsername());

        List<GroupDO> groupDOList = groupMapper.selectList(queryWrapper);

        if (CollUtil.isEmpty(groupDOList)) {
            throw new ServiceException("用户分组不存在");
        }
        requestParam.setGidList(groupDOList.stream().map(GroupDO::getGid).toList());
        return shortLinkRemoteService.pageRecycleBin(requestParam);
    }
}
